/**
 * Print entity.
 */
package models;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;

import play.data.validation.MaxSize;
import play.data.validation.Required;
import play.data.validation.Unique;
import play.db.jpa.Model;

@Entity
public class Print extends Model {	
	@Required
	@Column(name="file_name")
	public String fileName;
	
	@Column(name="name")
	@MaxSize(50)
	public String name;
	
	@Column(name="description")
	public String description; //default is empty

	@Required
	@Unique
	@Column(name="lc_path")
	public String lcPath;
	
	@Required
	@Column(name="source_url")
	public String sourceUrl;
		
	@Required
	@Column(name="thumbnail_s")
	public String thumbnailS;
	
	@Required
	@Column(name="thumbnail_m")
	public String thumbnailM;
	
	@Required
	@Column(name="thumbnail_l")
	public String thumbnailL;
	
	@Required
	@Column(name="link_expires")
	public Date linkExpires;
	
	@Column(name="client_m_time")
	public Date clientMtime;
	
	/**
	 * File modification date
	 */
	@Column(name="modified_file")
	public Date modifiedFile;
	
	/**
	 * Entity updated date 
	 */
	@Column(name="updated")
	public Date updated; //default is null
	
	@Column(name="enabled")
	public Boolean enabled; //default is false
	
	@Column(name="deleted")
	public Boolean deleted; //default is false
	
	public Long hits;
	
	@ManyToMany(cascade=CascadeType.PERSIST)
	public List<Group> groups;

	public Print(String fileName, String lcPath, Date clientMtime, Date modified, String sourceUrl,
			String thumbnailS, String thumbnailM, String thumbnailL, Date linkExpires, List<Group> groups) {
		super();
		this.fileName = fileName;
		this.lcPath = lcPath;
		this.clientMtime = clientMtime;
		this.modifiedFile = modified;
		this.sourceUrl = sourceUrl;
		this.thumbnailS = thumbnailS;
		this.thumbnailM = thumbnailM;
		this.thumbnailL = thumbnailL;	
		this.linkExpires = linkExpires;
		this.groups = groups;
		this.description = "";
		this.updated = null;
		this.enabled = false;
		this.deleted = false;
		this.hits = 0L;
	}

	public static List<Print> findAllEnabledByGroup(Integer groupId) {
		return Print
				.find("select distinct p from Print p join p.groups as g where g.groupId = ? and p.enabled = true and p.deleted = false order by p.hits desc",
						groupId).fetch();
	}

	@Override
	public String toString() {
		return "Print [fileName=" + fileName + ", name=" + name + ", description=" + description + ", lcPath=" + lcPath + ", sourceUrl=" + sourceUrl
				+ ", thumbnailS=" + thumbnailS + ", thumbnailM=" + thumbnailM + ", thumbnailL=" + thumbnailL + ", linkExpires=" + linkExpires
				+ ", clientMtime=" + clientMtime + ", modifiedFile=" + modifiedFile + ", updated=" + updated + ", enabled=" + enabled + ", deleted=" + deleted
				+ ", hits=" + hits + ", groups=" + groups + "]";
	}
	
	
}